<?php
    require APPPATH .'third_party/admin_controller'.EXT;
    class Category extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $data = array();
            //$data['list_cats'] = $this->category_model->list_cats_parent();
            $config['base_url'] =  base_url().'admin/category/index/';
			$uid =$this->uri->segment('4');            
			$config['per_page'] = '20'; 				
			$config['uri_segment'] = 4;
			//lay tong so trang
			$config['total_rows'] = $this->category_model->total();				
			$this->pagination->initialize($config);
			$data['page'] = $this->pagination->create_links();
			
			$data['list_cats'] = $this->category_model->product_phantrang($uid,$config['per_page']);
            
            $this->load_render($this->load->view('category/list',$data,TRUE));
        }
        public function add_update()
        {
            $data = array();
            $data['cats_parent'] = $this->category_model->list_cats_parent();
            $valid = $this->form_validation;
            $valid->set_rules('txt_name_cats','Title','trim|required|xss_clean');            
			$valid->set_rules('txt_url','URL','trim|required|xss_clean');
            $id = $this->uri->segment('4');
            if($valid->run() == TRUE)
            {
                $id_parent = $this->input->post('txt_id_parent') ?  $this->input->post('txt_id_parent') : 0;
                $order_cats = $this->input->post('txt_order_cats') ?  $this->input->post('txt_order_cats') : 0;
                $data = array(
                    'name_cats' => $this->input->post('txt_name_cats'),
                    'url'       => $this->input->post('txt_url'),
                    'active'    => $this->input->post('txt_active'),
                    'id_parent' => $id_parent,
                    'order_cats'=> $order_cats
                );
                if($id !='')
                {
                    if($this->category_model->update_cats_parent($id,$data))
                    {
                         redirect('admin/category/success');
                    }
                    else
                    {
                        $data['error'] = "Yêu cầu nhập thông tin đầy đủ";
                    }
                                        
                }
                else
                {
                    if($this->category_model->add_cats_parent($data))
                    {
                         redirect('admin/category/success');
                    }
                    else
                    {
                        $data['error'] = "Yêu cầu nhập thông tin đầy đủ";
                    } 
                }
               
            }
            
            $data['uid'] = $id;
            $data['cats'] = $this->category_model->get_cats_parent($id);
            $this->load_render($this->load->view('category/add_update',$data,TRUE));
        }
        public function success()
        {
            $data = array();
            $this->load_render($this->load->view('success',$data,TRUE));
        }
        function change_update_active()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->input->get('id');
            $data['active'] = $this->input->get('active');
            if($this->category_model->change_update_active($id,$data))
            {
                $data['error'] = "Cập nhật thành công";
            }
            echo json_encode($data);
        }
        public function delete_cats()
        {
            $data = array();
            $id = $this->uri->segment('4');
            if($this->category_model->delete_cats_parent($id))
            {
                redirect('admin/category');
            }
            
        }
    }
?>